#coding=utf-8
'''
Created on 2013年9月2日

@author: xuben

角色数据
'''

from ..common import HeartBeat, AIConfig
import FunctionData

#角色id
playerId = 0
#是否正在训练
isTrainning = False
#是否有跨服武斗
inkfwd = False
#整点在线奖励数
onlineRewardNum = 0

#银币
RES_TYPE_COPPER = 1
#木材
RES_TYPE_WOOD = 2
RES_TYPE_NAME_WOOD = ['wood', 'lumber']
#粮食
RES_TYPE_FOOD = 3
RES_TYPE_NAME_FOOD = ['food']
#镔铁
RES_TYPE_IRON = 4
RES_TYPE_NAME_IRON = ['iron']
#募兵令
RES_TYPE_NAME_RECRUIT_TOKEN = ['recruit_token']
#民忠
RES_TYPE_LOYALTY = 23

#金币数量
gold = 0
#银币数量
copper = 0
#木材数量
wood = 0
#粮食数量
food = 0
#镔铁数量
iron = 0
#当前经验值
exp = 0
#银币上限
copperMax = 0
#木材上限
woodMax = 0
#粮食上限
foodMax = 0

#其他数据
otherData = None

'''
初始化角色数据
'''
def init(data):
    global isTrainning
    global inkfwd
    global onlineRewardNum
    global otherData
    global gold
    global copper
    global wood
    global food
    global iron
    global copperMax
    global woodMax
    global foodMax
    if "isTrainning" in data:
        isTrainning = data["isTrainning"]
    if "inkfwd" in data:
        inkfwd = True
    if "onlineNum" in data:
        onlineRewardNum = data["onlineNum"]
    if "function" in data:
        FunctionData.resolve(data["function"])
    if "player" in data:
        if "pkey" in data["player"]:
            HeartBeat.pkey = data["player"]["pkey"]
        if "gold" in data["player"]:
            gold = data["player"]["gold"]
        if "copper" in data["player"]:
            copper = data["player"]["copper"]
        if "wood" in data["player"]:
            wood = data["player"]["wood"]
        if "food" in data["player"]:
            food = data["player"]["food"]
        if "iron" in data["player"]:
            iron = data["player"]["iron"]
        if "exp" in data["player"]:
            exp = data["player"]["exp"]
        if "copperMax" in data["player"]:
            copperMax = data["player"]["copperMax"]
        if "woodMax" in data["player"]:
            woodMax = data["player"]["woodMax"]
        if "foodMax" in data["player"]:
            foodMax = data["player"]["foodMax"]
    otherData = data

'''
更新角色数据
'''
def update(data):
    global onlineRewardNum
    global gold
    global copper
    global wood
    global food
    global iron
    global exp
    global copperMax
    global woodMax
    global foodMax
    if "update" in data:
        if "onlineNum" in data["update"]:
            onlineRewardNum = data["update"]["onlineNum"]
        if "gold" in data["update"]:
            gold = data["update"]["gold"]
        if "copper" in data["update"]:
            copper = data["update"]["copper"]
        if "wood" in data["update"]:
            wood = data["update"]["wood"]
        if "food" in data["update"]:
            food = data["update"]["food"]
        if "iron" in data["update"]:
            iron = data["update"]["iron"]
        if "exp" in data["update"]:
            exp = data["update"]["exp"]
        if "copperMax" in data["update"]:
            copperMax = data["update"]["copperMax"]
        if "woodMax" in data["update"]:
            woodMax = data["update"]["woodMax"]
        if "foodMax" in data["update"]:
            foodMax = data["update"]["foodMax"]
            
'''
更新多种资源
'''
def updateResources(data):
    if "output" in data:
        if "outputInfo" in data["output"]:
            for info in data["output"]["outputInfo"]:
                if "buildingType" in info and "realValue" in info:
                    updateResource(info["buildingType"], info["realValue"])

'''
更新单个资源
'''
def updateResource(rtype, rvalue):
    global copper
    global wood
    global food
    global iron
    if rtype == RES_TYPE_COPPER:
        copper = rvalue
    elif rtype == RES_TYPE_WOOD:
        wood = rvalue
    elif rtype == RES_TYPE_FOOD:
        food = rvalue
    elif rtype == RES_TYPE_IRON:
        iron = rvalue
        
'''
获取资源优先级列表
'''
def getResourcePriority():
    priority = {RES_TYPE_FOOD:1, RES_TYPE_WOOD:2, RES_TYPE_COPPER:3}
    idx = len(priority)
    #粮食满仓
    if food >= foodMax:
        idx += 1
        priority[RES_TYPE_FOOD] = idx
    #木材满仓
    if wood >= woodMax:
        idx += 1
        priority[RES_TYPE_WOOD] = idx
    #银币满仓
    if copper >= copperMax:
        idx += 1
        priority[RES_TYPE_COPPER] = idx
    return priority

'''
获取停止攻城的粮食数
'''
def getAttackStopFood():
    return min(AIConfig.attack_stop_food, foodMax * AIConfig.attack_stop_food_relative)\
        if AIConfig.attack_stop_food_use_min\
        else max(AIConfig.attack_stop_food, foodMax * AIConfig.attack_stop_food_relative)